import QtQuick 2.0
import QtQuick.Controls 2.5
import com.stupidzhe.QrCode 1.0

Item {
    id: root
    anchors.fill: parent
    opacity: 0

    signal readyToGo

    property bool loginViewIsReady: false
    property bool loginFinish: false
    property bool ready: loginViewIsReady && loginFinish
    onReadyChanged: {
        if (ready)
            readyToGo()
    }
    function appReady() {
        loginViewIsReady = true
    }

    function errorInLoadingApp() {
        Qt.quit()
    }

    function init() {
        opAnimation.start()
    }

    Loader {
        id: emitter
        anchors.fill: parent
        asynchronous: false
        visible: true
    }

    // index view
    Column {
        anchors.centerIn: parent
        Image {
            anchors.horizontalCenter: parent.horizontalCenter
            id: logo
            width: 90
            height: 90
            source: "qrc:/img/res/logo.png"
            fillMode: Image.PreserveAspectFit
            signal clicked
            onClicked: {
                console.log(1)
            }
            MouseArea {
                anchors.fill: parent
                onClicked: logo.clicked()
            }
        }
        Label {
            id: expression
            anchors.horizontalCenter: parent.horizontalCenter
            width: 237
            height: 56
            text: "Go on WeFuck\nvia\nScanning to the QR Code."
            font.bold: true
            font.pointSize: 17
            font.family: "Courier"
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
        }
        Rectangle {
            id: boarder
            width: 154
            height: 154
            border.color: "black"
            border.width: 2
            anchors.horizontalCenter: parent.horizontalCenter

            Image {
                width: 150
                height: 150
                anchors.centerIn: parent
                property string router: "https://wx.qq.com/qrcode/"
                anchors.rightMargin: 0
                anchors.bottomMargin: 0
                anchors.leftMargin: 0
                anchors.topMargin: 0
                id: qrImage

                //   source: this.router
                fillMode: Image.PreserveAspectFit

                Label {
                    id: loadingLabel
                    x: 35
                    y: 67
                    color: "#2b2626"
                    text: qsTr("please wait...")
                    z: -1
                }
            }
        }
        spacing: 30
        Label {
            id: coop
            anchors.horizontalCenter: parent.horizontalCenter
            text: qsTr("©Copyright by StupidZhe")
            font.family: "Courier"
            lineHeight: 1.2
        }
    }

    Connections {
        target: qrCode
        onUuidChanged: {
            qrImage.router += qrCode.uuid
            qrImage.source = qrImage.router
        }
    }

    Connections {
        target: qrCode
        onImgUrlChanged: {
            qrImage.source = qrCode.imgUrl
            expression.text = "Please confirm\nin your Wechat App"
            emitter.setSource("LoginActiveEmitter.qml")
        }
        onStatusChanged: {
            if (qrCode.status === QrCode.CONFIRM) {
                loginFinish = true
                console.log("login success!" + ready)
            }
        }
    }

    // 动画
    NumberAnimation {
        id: opAnimation
        target: root
        property: "opacity"
        duration: 200
        to: 1
    }

    Component.onCompleted: {
        appReady()
    }
}
